﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Reflection;
using CloudShot.ImageStorages;

namespace CloudShot.Utils
{
  static class AppHelpers
  {
    public static StorageSettings ToStorageSettings( this IImageStorage storage )
    {
      return new StorageSettings
      {
        StorageType = storage.GetType(),
        ImageFormat = storage.CurrentImageFormat,
        FolderToSave = storage.FolderToSave
      };
    }

    public static IEnumerable<Type> FindDerivedTypesFromAssembly( this Assembly assembly, Type baseType )
    {
      if ( assembly == null )
        throw new ArgumentNullException( "assembly", "Assembly must be defined" );

      if ( baseType == null )
        throw new ArgumentNullException( "baseType", "Parent Type must be defined" );

      // get all the types
      var types = assembly.GetTypes();

      // works out the derived types
      foreach ( var type in types )
      {
        // it must be a class
        if ( !type.IsClass || type.IsAbstract )
          continue;

        if ( baseType.IsInterface )
        {
          var it = type.GetInterface( baseType.FullName );

          if ( it != null )
            // add it to result list
            yield return type;
        }
        else if ( type.IsSubclassOf( baseType ) )
        {
          // add it to result list
          yield return type;
        }
      }
    }

		public static string GetFileName(ImageFormat imageFormat)
		{
			string date = FileUtils.EscapeFilename(DateTime.Now.ToString("G"), "").Replace(".", "").Replace(' ', '_');
			return string.Format("shot_{0}.{1}", date, imageFormat).ToLower();
		}
  }
}
